﻿#region Copyright Notice
// This file is part of GestionPedago.
// <description>
// Copyright (C) 2012 ENI Ecole Informatique
// 
// GestionPedago is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// <program> is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GestionPedago.Services.Framework;
using GestionPedago.Services.ES;
using GestionPedago.Services.ES.Modele;
using GestionPedago.Services.Mapping;
using GestionPedago.EntityContext;
using Persistence.OPManager;
using GestionPedago.Persistence.OPManager;

namespace GestionPedago.Services.Traitement
{
    /// <summary>
    /// Classe de traitement du service RecupererPlanningECF
    /// </summary>
    public class RecupererPlanningECFTraitement : ITraitement<RecupererPlanningECFIn, RecupererPlanningECFOut>
    {
        /// <summary>
        /// Singleton
        /// </summary>
        private static RecupererPlanningECFTraitement instance;
        
        /// <summary>
        /// Méthode GetInstance
        /// </summary>
        /// <returns>Le singleton</returns>
        public static RecupererPlanningECFTraitement GetInstance()
        {
            if (instance == null)
            {
                instance = new RecupererPlanningECFTraitement();
            }
            return instance;
        }

        /// <summary>
        /// Constructeur
        /// </summary>
        private RecupererPlanningECFTraitement() { }

        /// <summary>
        /// Méthode de traitement
        /// </summary>
        /// <param name="paramEntree">Le paramètre d'entrée du service</param>
        /// <returns>Le paramètre de sortie du service</returns>
        public RecupererPlanningECFOut Traitement(RecupererPlanningECFIn paramEntree)
        {

            EntityConnection.Reinitialisation();
            
            List<PlanningECFES> lst = new List<PlanningECFES>();

            //Promotion p = new PromotionMappeur().ES2Entity(paramEntree.Promotion);
            
            List<PlanningECF> lstPlanningPromotion = new List<PlanningECF>();
            Promotion p = null;
            if (paramEntree.Promotion.CodePromotion == "HorsPromo")
            {
                lstPlanningPromotion =  PlanningECFOPManager.GetListePlanningECFHorsPromotion();
            }
            else
            {
                p = PromotionOPManager.GetPromotionByCode(paramEntree.Promotion.CodePromotion);
                lstPlanningPromotion = p.GetListePlanificationsECF();
            }
                
            foreach (PlanningECF r in lstPlanningPromotion)
            {
                // Mapping du stagiaire Entity vers l'ES et ajout dans la liste
                PlanningECFES pee = new PlanningECFMappeur().Entity2ES(r);
                pee.DateSuggeree = pee.DateECF;
                pee.DejaPlanifie = true;
                lst.Add(pee);
                EntityConnection.GetContext().PlanningECF.Detach(r);
            }

            if (paramEntree.Promotion.CodePromotion != "HorsPromo")
            {
                paramEntree.Promotion.ListeCours.Sort(new CoursESComparer());
                foreach (ECF e in p.GetListeECFAPasser())
                {
                    PlanningECFES pee = new PlanningECFES();
                    pee.IdECF = e.IdECF;
                    pee.ECF = new ECFMappeur().Entity2ES(e);
                    pee.CodePromotion = paramEntree.Promotion.CodePromotion;
                    pee.Promotion = paramEntree.Promotion;
                    pee.DateSuggeree = e.GetSuggestionDate(p);
                    pee.DejaPlanifie = false;
                    lst.Add(pee);
                    EntityConnection.GetContext().ECF.Detach(e);
                }
            }
          
            // Création de l'ES de sortie avec la liste obtenue
            return new RecupererPlanningECFOut(lst);
        }
    }
}
